/*下载链接
彭济
9-24*/
import BasicController from './../BasicController'
import ErrorHandlerCode from './../../util/ErrorHandlerCode'
import {DownloadModel} from '../../model'

class DownloadController extends BasicController{
    findUrlById(req, res, next){
    	if(!super.isEmptyParameter(req.body.id)){
    		res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'id 为必填参数'
            })
            return;
    	}
        DownloadModel.urlFindById(req.body.id).then((data) => {
            super.RequestSuccessMsg(res, data);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
    }
    findAllUrl(req,res,next){
    	DownloadModel.findAllUrl().then((data) => {
            super.RequestSuccessMsg(res, data);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        });
    }
    pagingUrl(req,res,next){
    	if(!super.isEmptyParameter(req.body.page_size)){
    		req.body.page_size = 10;
    	}
    	if(!super.isEmptyParameter(req.body.page_no)){
    		req.body.page_no = 1;
    	}
    	DownloadModel.urlPaging(req.body).then((data) => {
            super.RequestSuccessMsg(res, data);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        });
    }
    async insertUrl(req,res,next){
    	if(!super.isEmptyParameter(req.body.download_title)){
    		res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'download_title 为必填参数'
            })
            return;
    	}
    	if(!super.isEmptyParameter(req.body.type)){
    		res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'type 为必选参数'
            })
            return;
    	}
    	if(!super.isEmptyParameter(req.body.download_version)){
    		res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'download_version 为必填参数'
            })
            return;
    	}
    	if(!super.isEmptyParameter(req.body.download_url)){
    		res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'download_url 为必填参数'
            })
            return;
    	}
    	if(!super.isEmptyParameter(req.body.download_url)){
    		res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'download_url 为必填参数'
            })
            return;
    	}
    	if(!super.isEmptyParameter(req.body.is_enable)){
    		res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'is_enable 为必填参数'
            })
            return;
    	}
    	if(!super.isEmptyParameter(req.body.is_enable)){
    		res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'is_enable 为必选参数'
            })
            return;
    	}
    	if(req.body.is_enable == 1){
    		let setEnable = DownloadModel.updateByEnable(req.body);
    	}
    	DownloadModel.insertUrl(req.body).then((data) =>{
            super.RequestSuccessMsg(res, data);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
    }
    async updateUrl(req,res){
    	if(req.body.is_enable == 1){
    		let setEnable = await DownloadModel.updateByEnable(req.body);
    	}
    	DownloadModel.urlUpdateById(req.body).then((data) =>{
            super.RequestSuccessMsg(res, data);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
    }
    deleteUrlById(req,res){
    	if(!super.isEmptyParameter(req.body.id)){
    		res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'id 为必选参数'
            })
            return;
    	}
        DownloadModel.urlDelById(req.body.id).then((data) =>{
            super.RequestSuccessMsg(res,data);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
    }
    findUrlByEnable(req,res){
        if(!super.isEmptyParameter(req.body.is_enable)){
            res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'is_enable 为必选参数'
            })
            return;
        }
        DownloadModel.findByEnable(req.body.is_enable).then((data) =>{
            super.RequestSuccessMsg(res,data);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
    }
}
module.exports = new DownloadController();